package activity_disciplinas;

import util.TableHeaderTextView;
import util.TableRowTextView;
import activity_atividades.Atividades;
import activity_provas.Provas;
import activity_trabalhos.Trabalhos;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.adjt.droidsapiens.MainHome;
import com.adjt.droidsapiens.R;

import database.DatabaseConnector;

@SuppressLint("SimpleDateFormat")
public class Disciplinas extends Activity {

	Intent it;
	int semestreAtual;
	public static final int ATIVIDADES = 0;
	public static final int PROVAS = 1;
	public static final int TRABALHOS = 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_disciplinas);
		
		semestreAtual = getIntent().getIntExtra("semestreAtual", 0);
		
		DatabaseConnector db = new DatabaseConnector(getBaseContext());
		Cursor c;
		db.open();
		String tables = "Disciplina";
		String[] column = new String[]{"idDisciplina, nome"};
		String where = "idSemestre = " + semestreAtual;
		String orderBy = "";
		
		Resources r = getResources();
		int pxw = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, r.getDisplayMetrics());
		int pxh = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 36, r.getDisplayMetrics());
		
		TableLayout layout = (TableLayout) findViewById(R.id.Disciplinas_TableLayout);
		
		c = db.buscar(tables, column, where, orderBy);
		if (c.getCount() > 0) {
			TableRow header = new TableRow(this);
			header.setGravity(Gravity.CENTER);
			
			TableHeaderTextView labelnome = new TableHeaderTextView(this);
			labelnome.setText(R.string.nome);
			labelnome.setWidth(pxw);
			header.addView(labelnome);
			
			TextView vazio = new TextView(this);
			header.addView(vazio);
			
			layout.addView(header);
			
			while(c.moveToNext()){
				
				TableRow tr = new TableRow(this);
				tr.setGravity(Gravity.CENTER);
				
				TableRowTextView nomeDisciplinaTxt = new TableRowTextView(this); 
				nomeDisciplinaTxt.setText(c.getString(c.getColumnIndex("nome")));
				
				ImageButton verDisciplina = new ImageButton(this);
				verDisciplina.setTag(c.getString(c.getColumnIndex("idDisciplina")));
				verDisciplina.setBackgroundColor(Color.TRANSPARENT);
				verDisciplina.setBackgroundResource(R.drawable.ic_view);
				verDisciplina.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						it = new Intent(getBaseContext(), ExibirDisciplina.class);
						it.putExtra("idDisciplina", Integer.parseInt(v.getTag().toString()));
						it.putExtra("semestreAtual", semestreAtual);
						startActivity(it);
						finish();
					}
				});
				
				nomeDisciplinaTxt.setHeight(pxh);
				tr.addView(nomeDisciplinaTxt);
				tr.addView(verDisciplina);
				
				layout.addView(tr);
			}
		}
		else {
			TableRow tr = new TableRow(this);
			tr.setGravity(Gravity.CENTER);
			
			TextView listaVazia = new TextView(this);
			listaVazia.setText(R.string.listaDisciplinasVazia);
			tr.addView(listaVazia);
			
			layout.addView(tr);
		}
		db.close();
	}
	
	public void goNovaDisciplina(View v){
		it = new Intent(getBaseContext(),NovaDisciplina.class);
		it.putExtra("semestreAtual", semestreAtual);
		startActivity(it);
		finish();
	}
	
	@Override
	public void onBackPressed(){
		it = new Intent(getBaseContext(), MainHome.class);
		it.putExtra("semestreAtual", semestreAtual);
		startActivity(it);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, ATIVIDADES, 1, R.string.atividades);
		menu.add(0, PROVAS, 2, R.string.provas);
		menu.add(0, TRABALHOS, 3, R.string.trabalhos);
				
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featuredId, MenuItem item){
		
		switch(item.getItemId()){
		
			case ATIVIDADES:
				it = new Intent(getBaseContext(), Atividades.class);
				it.putExtra("semestreAtual", semestreAtual);
				startActivity(it);
				finish();
				return true;
			  
			case PROVAS:
				it = new Intent(getBaseContext(), Provas.class);
				it.putExtra("semestreAtual", semestreAtual);
				startActivity(it);
				finish();
				return true;
				  
			case TRABALHOS:
				it = new Intent(getBaseContext(), Trabalhos.class);
				it.putExtra("semestreAtual", semestreAtual);
				startActivity(it);
				finish();
				return true;	  
		
		}
		return false;
		
	}
}
